<?php

namespace common\modules\sms\models\backend;

use common\widgets\grid\CheckboxColumn;
use common\widgets\GridView;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;

/**
 * EventSearch represents the model behind the search form about `common\modules\sms\models\backend\Event`.
 */
class EventSearch extends Event
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'service_id', 'customer_id', 'status', 'created_at', 'updated_at', 'created_by', 'updated_by'],
                'integer'],
            [['mobile', 'content'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/sms', 'Event');
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Event::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if ( ! $this->validate())
        {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id'          => $this->id,
            'service_id'  => $this->service_id,
            'customer_id' => $this->customer_id,
            'status'      => $this->status,
            'created_at'  => $this->created_at,
            'updated_at'  => $this->updated_at,
            'created_by'  => $this->created_by,
            'updated_by'  => $this->updated_by,
        ]);

        $query->andFilterWhere(['like', 'mobile', $this->mobile])
            ->andFilterWhere(['like', 'content', $this->content]);

        return $dataProvider;
    }

    /**
     * 返回字段的定义信息
     *
     * @return array
     */
    public function gridViewColumns()
    {
        return [

            [
                'attribute' => 'id',
            ],

            [
                'attribute' => 'customer_id',
                'value' => function($model) {
                    /** @var Event $model */
                    if ($model->customer)
                    {
                        return $model->customer->name;
                    }
                    return $model->customer_id;
                },
                'format' => 'html',
                'width' => '150px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => ArrayHelper::map(Customer::find()->all(), 'id', 'name'),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '选择客户'],
            ],

            'mobile',

            'content',

            [
                'attribute' => 'status',
                'value' => function($model) {
                    /** @var Event $model */
                    if ($model->status = self::STATUS_ON)
                    {
                        return '成功';
                    }
                    elseif ($model->status = self::STATUS_OFF)
                    {
                        return '失败';
                    }
                    return '未知';
                },
                'format' => 'html',
                'width' => '100px',
                'filterType' => GridView::FILTER_SELECT2,
                'filter' => self::statusArray(),
                'filterWidgetOptions' => [
                    'pluginOptions' => ['allowClear' => true],
                ],
                'filterInputOptions' => ['placeholder' => '状态'],
            ],

            [
                'attribute'  => 'created_at',
                'format'     => ['date', 'php:Y-m-d H:i:s'],
                'width' => '190px',
            ],

            [
                'class' => 'common\widgets\grid\ActionColumn',
            ],
        ];
    }
}
